//
//  UserAccount.swift
//  Weibo
//
//  Created by mac on 15/11/28.
//  Copyright © 2015年 zsl. All rights reserved.
//

import UIKit

class UserAccount: NSObject,NSCoding {
    
    /*
    {
    "access_token": "ACCESS_TOKEN",
    "expires_in": 1234,
    "remind_in":"798114",
    "uid":"12341234"
    }
    */
    //计算型属性 计算token失效的日期
    //开发者账号的有效期 是5年
    //普通用户的有效期 是 3天  未审核通过的token的有效期 是1天
    var expires_date: NSDate?

    ////用于调用access_token，接口获取授权后的access token。
    var access_token: String?
    //access_token的生命周期，单位是秒数。
    var expires_in: NSTimeInterval = 0{
        
        didSet{
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    //当前授权用户的uID
    var uid: String?
    
    //好友显示名称
    var name: String?
    var avatar_large: String?
    
    init(dict:[String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
     //mark:保存自定义的对象
  func saveAccount(){
    
    let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
    
    NSKeyedArchiver.archiveRootObject(self, toFile: path)
    
    print(path)
    
    }
    
     //mark:从磁盘中获取自定义对象
    class func loadAccount() -> UserAccount? {
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        //解当
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount{
            
            if account.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending
            {
               return account
            }
        }
        
        return nil
    }
    
    
    /**
    *  过滤
    */
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
        
    /**
    *  重写description方法
    */
    override var description: String{
        
        let keys = ["access_token","expires_in","uid","name","avatar_large","expires_date"];
    
    return dictionaryWithValuesForKeys(keys).description
    
    }
    
     //mark:实现NSCoding 的协议方法 解归档将二进制数据->对象  反序列化
    required init?(coder aDecoder:NSCoder){
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
        
    }
    
    //归档 将对象转换成二进制数据存储在沙河,序列化
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey:"access_token")
        aCoder.encodeObject(expires_date, forKey:"expires_date")
        aCoder.encodeObject(uid, forKey:"uid")
        aCoder.encodeObject(name, forKey:"name")
        aCoder.encodeObject(avatar_large, forKey:"avatar_large")
        
        
    }
    
}
